
package org.japril.jutil.uri;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectOutputStream;
import java.net.URI;

/**
 * <p>
 * </p>
 * @author Arne Halvorsen (AH)
 */
public final class JavaObjectUriFetcher extends UriInputStreamFetcher 
{
  public JavaObjectUriFetcher(){ super("java-object"); }

  @Override
  protected InputStream fetch_(URI uri) throws IOException
  {
    try
    {
      //String cname=uri.getPath();
      String cname=uri.getSchemeSpecificPart();
      System.out.println("cname: "+cname);
      Class<?> clazz=Class.forName(cname);
      Object o=clazz.newInstance();
      ByteArrayOutputStream baos=new ByteArrayOutputStream();
      ObjectOutputStream dos=new ObjectOutputStream(baos);
      dos.writeObject(o);
      dos.flush();
      byte[] obytes=baos.toByteArray();
      ByteArrayInputStream bais=new ByteArrayInputStream(obytes);
      return bais;
    }
    catch (ClassNotFoundException cnfx)
    {
      return null;
    }
    catch (InstantiationException ix)
    {
      IOException iox=new IOException();
      iox.initCause(ix);
      throw iox;
    }
    catch (IllegalAccessException iax)
    {
      IOException iox=new IOException();
      iox.initCause(iax);
      throw iox;
    }
  }
  
}
